﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

using mvcForum.Core;
using mvcForum.Core.Interfaces;
using mvcForum.Web.Attributes;
using mvcForum.Web.Controllers;
using mvcForum.Web.Extensions;
using mvcForum.Web.Helpers;
using mvcForum.Web.ViewModels;
using mvcForum.Web.Interfaces;
using mvcForum.Core.Interfaces.Services;
using mvcForum.Core.Abstractions.Interfaces;
using mvcForum.Core.Interfaces.Data;

namespace mvcForum.Web.Areas.Forum.Controllers {

	public class FileController : ThemedForumBaseController {
		private readonly IAttachmentService attachmentService;
		private readonly IConfiguration config;

		public FileController(IWebUserProvider userProvider,
								IContext context,
								IAttachmentService attachmentService,
								IConfiguration config)
			: base(userProvider, context) {

			this.attachmentService = attachmentService;
			this.config = config;
		}

		[Authorize]
		public ActionResult Attach(Int32 id) {
			Post post = this.GetRepository<Post>().Read(id);

			// Is the user allowed to upload?
			if (post.Topic.Forum.HasAccess(AccessFlag.Upload)) {
				// For now, only the author can post files to a message!
				if (this.ActiveUser.Id == post.Author.Id) {
					Dictionary<String, String> path = new Dictionary<String, String>();
					// Build the breadcrumb path.
					HomeController.BuildPath(post.Topic, path, this.Url);
					return View(new MessageViewModel(post, this.config.AuthorNameSetting) { /*Message = message,*/ Path = path, Topic = new TopicViewModel(post.Topic, new MessageViewModel[] { }, 0, 1, false, this.config.AllowQuickReply, this.config.AuthorNameSetting) /*{ Topic = message.Topic }*/ });
				}
			}

			// No, the user is not allowed to upload, let's inform him!
			TempData.Add("Reason", ForumHelper.GetString("NoAccess.NoUpload"));
			// Off to the "no access" page!
			return RedirectToRoute("NoAccess");
		}

		[HttpPost]
		[Authorize]
		[NotBanned]
		public ActionResult Attach(Int32 id, HttpPostedFileBase[] files) {
			// Get the post the file should be attached to.
			Post post = this.GetRepository<Post>().Read(id);

			// Is the user allowed to upload?
			if (post.Topic.Forum.HasAccess(AccessFlag.Upload)) {
				// For now, only the author can post files to a message!
				if (this.ActiveUser.Id == post.Author.Id) {
					List<String> feedback = new List<String>();
					foreach (HttpPostedFileBase file in files) {
						if (file != null) {
							AttachStatusCode returnValue = this.attachmentService.AttachFile(this.ActiveUser, post, file.FileName, file.ContentType, file.ContentLength, file.InputStream);
							if (returnValue != AttachStatusCode.Success) {
								feedback.Add(ForumHelper.GetString(returnValue.ToString(), new { File = file.FileName, Size = file.ContentLength, MaxFileSize = this.config.MaxFileSize, MaxAttachmentsSize = this.config.MaxAttachmentsSize, Extensions = this.config.AllowedExtensions }, "mvcForum.Web.AttachmentErrors"));
							}
						}
					}

					if (feedback.Any()) {
						TempData.Add("Feedback", feedback.Select(f => new MvcHtmlString(f)));
					}

					// File(s) uploaded, let's get back to the topic.
					return new RedirectToRouteResult("ShowTopic", new RouteValueDictionary { { "id", post.Topic.Id }, { "title", post.Topic.Title.ToSlug() } });
				}
			}

			// No, the user is not allowed to upload, let's inform him!
			TempData.Add("Reason", ForumHelper.GetString("NoAccess.NoUpload"));
			// Off to the "no access" page.
			return RedirectToRoute("NoAccess");
		}
	}
}